﻿using mozhi.smarterp.Business.Enums;
using mozhi.smarterp.Business.Inventories.Repositories;
using mozhi.smarterp.Business.Products.Repositories;
using Mozhi.Abp.Domain.BasicData;
using Mozhi.Abp.Domain.BasicData.Repositories;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Services;
using Volo.Abp.Guids;

namespace mozhi.smarterp.Business.Products.Services
{
    public class ProductInventorySettingManager : DomainService
    {
        private IProductRepository _productRepository;
        private IWarehouseRepository _warehouseRepository;
        private IGuidGenerator _guidGenerator;
        private IProductInventorySettingRepository _repository;
        private IInventoryRealTimeRepository _inventoryRealTimeRepository;

        public ProductInventorySettingManager(IProductRepository productRepository, IWarehouseRepository warehouseRepository, IGuidGenerator guidGenerator,
            IProductInventorySettingRepository repository, IInventoryRealTimeRepository inventoryRealTimeRepository)
        {
            _productRepository = productRepository;
            _warehouseRepository = warehouseRepository;
            _guidGenerator = guidGenerator;
            _repository = repository;
            _inventoryRealTimeRepository = inventoryRealTimeRepository;
        }

        public async Task<ProductInventorySetting> GetAsync(Guid? id, Guid productId, Guid warehousId, Guid? locationId, ProductPricingType pricingType, bool enableBatchNumber,
            bool enableShelfLife, decimal? shelfLifeDays, bool enableSerialNumber, decimal? minStockQuantity, decimal? maxStockQuantity, decimal? safetyStockQuantity, decimal? reorderPointQuantity)
        {
            var matchCount = await _repository.GetCountAsync(productId, id);

            if (matchCount > 0)
                throw new UserFriendlyException($"产品：{productId}库存信息已存在", "100000");

            var product = await _productRepository.GetAsync(productId);
            var warehouse = await _warehouseRepository.GetAsync(warehousId);

            ProductInventorySetting inventorySetting;

            if (id.HasValue)
            {
                inventorySetting = await _repository.GetAsync(id.Value);
                if (inventorySetting == null)
                    throw new UserFriendlyException($"产品库存设置：{id.Value}不存在");

                await ValidateAsync(inventorySetting, pricingType, enableBatchNumber, enableShelfLife, enableSerialNumber);

                inventorySetting.Change(product, pricingType, warehouse, locationId, enableBatchNumber, enableShelfLife, shelfLifeDays, enableSerialNumber,
                    minStockQuantity, maxStockQuantity, safetyStockQuantity, reorderPointQuantity);
            }
            else
                inventorySetting = new ProductInventorySetting(_guidGenerator.Create(), product, pricingType, warehouse, locationId, enableBatchNumber, enableShelfLife, shelfLifeDays, enableSerialNumber,
                    minStockQuantity, maxStockQuantity, safetyStockQuantity, reorderPointQuantity);

            return inventorySetting;
        }

        public async Task ValidateAsync(ProductInventorySetting inventorySetting, ProductPricingType pricingType, bool enableBatchNumber,
            bool enableShelfLife, bool enableSerialNumber)
        {
            var inventoryCount = await _inventoryRealTimeRepository.GetCountAsync(inventorySetting.ProductId, null, null, null, null, null);

            if (inventoryCount == 0)
                return;

            if (inventorySetting.PricingType != pricingType)
                throw new UserFriendlyException($"产品：{inventorySetting.ProductId}存在库存数据，不能更改计价方式", "100000");

            if (inventorySetting.EnableBatchNumber != enableBatchNumber)
                throw new UserFriendlyException($"产品：{inventorySetting.ProductId}存在库存数据，不能{(inventorySetting.EnableBatchNumber ? "关闭" : "启用")}批次", "100000");

            if (inventorySetting.EnableSerialNumber != enableSerialNumber)
                throw new UserFriendlyException($"产品：{inventorySetting.ProductId}存在库存数据，不能{(inventorySetting.EnableSerialNumber ? "关闭" : "启用")}序列号", "100000");

            if (inventorySetting.EnableShelfLife != enableShelfLife)
                throw new UserFriendlyException($"产品：{inventorySetting.ProductId}存在库存数据，不能{(inventorySetting.EnableSerialNumber ? "关闭" : "启用")}保质期", "100000");

        }
    }
}
